package pers.jason.wppm.auth.log;

import lombok.extern.log4j.Log4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * @ClassName: ControllerLogger
 * @Author: JZH
 * @Date: 2018/12/25 15:55
 *
 * 为controller层的调用做日志
 */
@Log4j
@Aspect
@Component
public class ControllerLogger {

    @Pointcut("@annotation(pers.jason.wppm.auth.log.ControllerLog)")
    public  void controllerAspect() {
    }

    @Before("controllerAspect()")
    public void doBefore(JoinPoint joinPoint) {
        String targetName = joinPoint.getTarget().getClass().getName();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String name = method.getName();
        String description = method.getAnnotation(ControllerLog.class).description();
        log.info(LocalDateTime.now() + ": 请求【" + targetName+"."+name+"()】 描述：" + description);
    }

}
